<link rel="stylesheet" href="/assets/home/css/profile.css" />
<link rel="stylesheet" href="/assets/home/plugin/citypicker/css/city-picker.css" />
<script src="/assets/home/plugin/citypicker/js/city-picker.data.min.js"></script>
<script src="/assets/home/plugin/citypicker/js/city-picker.min.js"></script>


<header class="mui-bar mui-bar-nav mui-bar-nav-bg">
    <a id="icon-menu" class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">基本资料</h1>
</header>

<div class="user">
    <div class="avatar wow fadeInUp">
        {if condition="$AutoLogin['avatar'] && is_file('.'.$AutoLogin['avatar'])"}
            <img src="{$AutoLogin.avatar}" />
        {else /}
            <img src="/assets/home/images/avatar.jpg" />
        {/if}
    </div>
</div>

<form class="mui-input-group" method="post" enctype="multipart/form-data">
    <div class="mui-input-row">
        <label>昵称：</label>
        <input type="text" name="nickname" value="{$AutoLogin.nickname}" class="mui-input-clear" placeholder="请输入昵称" required />
    </div>
    <div class="mui-input-row">
        <label>手机号码：</label>
        <input type="text" name="mobile" value="{$AutoLogin.mobile}"  class="mui-input-clear" placeholder="请输入手机号码" required />
    </div>
    <div class="mui-input-row">
        <label>邮箱：</label>
        <input type="email" name="email" value="{$AutoLogin.email}"  class="mui-input-clear" placeholder="请输入邮箱" required />
    </div>
    <div class="mui-input-row">
        <label>密码：</label>
        <input type="password" name="password" class="mui-input-password" placeholder="请输入密码">
    </div>
    <div class="mui-input-row">
        <label>性别：</label>
        <select name="gender">
            <option {$AutoLogin['gender'] == "0" ? "selected" : ''} value="0">保密</option>
            <option {$AutoLogin['gender'] == "1" ? "selected" : ''} value="1">男</option>
            <option {$AutoLogin['gender'] == "2" ? "selected" : ''} value="2">女</option>
        </select>
    </div>
    <div class="mui-input-row region">
        <label>地区：</label>
        <div class="citypicker">
            <input id="city" type="text" data-toggle="city-picker" name="region" readonly />
        </div>
    </div>

    <input type="file" class="form-control" id="avatar" name="avatar" style="display: none;" />
    
    <div class="mui-button-row">
        <button type="submit" class="mui-btn mui-btn-primary">确认</button>
    </div>
</form>

<script>
    // 获取登录信息的省市区
    let province = "{$AutoLogin.province ? $AutoLogin.province : ''}";
    let city = "{$AutoLogin.city ? $AutoLogin.city : ''}";
    let district = "{$AutoLogin.district ? $AutoLogin.district : ''}";

    // 赋默认值
    let code = parseInt(district) || parseInt(city) || parseInt(province);
    $("#city").val(code);

    $('#city').citypicker();

    //更改地区码的值
    $("#city").on("cp:updated", function(){
        var citypicker = $(this).data('citypicker')
        var region = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province")
        $("input[name=region]").val(region)
    })

    //选择头像
    $('.avatar').click(function(){
        $('#avatar').click()
    })

    //给Input元素绑定一个改变的事件
    $('#avatar').change(function(){
        //将拿到的图片，转换为本地的图片结构，进行显示
        // console.log($(this)[0].files[0])

        //选择的图片
        var file = $(this)[0].files[0]

        //创建一个文件读取器对象
        var reader = new FileReader()

        //让这个对象去读取上面的文件
        reader.readAsDataURL(file)

        //绑定一个事件，读取成功后拿到结果的事件
        reader.onload = function(ev)
        {
            //把base64的数据 渲染上去
            $(".avatar img").attr('src', ev.target.result)
        }
    })
</script>